package Chapter05;

import java.util.HashMap;
import java.util.Map;

public class CarRentalSystem {

        private Map<String, Integer> carMap;
        private Map<String, Integer> rentedCarMap;

        public CarRentalSystem() {
            carMap = new HashMap<String, Integer>();
            rentedCarMap = new HashMap<String, Integer>();
        }

        public void addCar(String model, int quantity) {
            carMap.put(model, quantity);
        }

        public void rentCar(String model, int days) {
            if (carMap.containsKey(model)) {
                int quantity = carMap.get(model);
                if (quantity > 0) {
                    carMap.put(model, quantity - 1);
                    rentedCarMap.put(model, days);
                    System.out.println("The car " + model + " has been rented for " + days + " days.");
                } else {
                    System.out.println("Sorry, the car " + model + " is out of stock.");
                }
            } else {
                System.out.println("Sorry, the car " + model + " is not available.");
            }
        }

        public void returnCar(String model) {
            if (rentedCarMap.containsKey(model)) {
                int days = rentedCarMap.get(model);
                rentedCarMap.remove(model);
                int quantity = carMap.get(model);
                carMap.put(model, quantity + 1);
                System.out.println("The car " + model + " has been returned after " + days + " days.");
            } else {
                System.out.println("Sorry, the car " + model + " was not rented from this rental service.");
            }
        }

        public void displayStock() {
            System.out.println("Car Stock:");
            for (Map.Entry<String, Integer> entry : carMap.entrySet()) {
                System.out.println(entry.getKey() + " - " + entry.getValue() + " cars");
            }
        }

        public static void main(String[] args) {
            CarRentalSystem rentalService = new CarRentalSystem();
            rentalService.addCar("Toyota Camry", 5);
            rentalService.addCar("Honda Accord", 3);
            rentalService.addCar("Nissan Altima", 2);
            rentalService.displayStock();
            rentalService.rentCar("Toyota Camry", 3);
            rentalService.rentCar("Honda Civic", 2);
            rentalService.returnCar("Toyota Camry");
            rentalService.displayStock();
        }
}

